<?php
namespace app\restaurant\model;

use app\BaseModel;
use app\member\model\Member;
use app\restaurant\server\Context;
use longbingcore\wxcore\DadaApi;
use think\facade\Db;

class Order extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_order';


    protected $append = [

        'order_goods',

        'total_num',

        'store_mobile',

        'out_foods_status',

        'now_progress'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-11-03 11:43
     * @功能说明:订单当前进度
     */
    public function getNowProgressAttr($value,$data){

        if(!empty($data['order_code'])){

            $progress_model = new Progress();

            $arr = $progress_model->nowOrderText($data['order_code']);

            return $arr;

        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:判断订单是不是待接单状态
     * @author chenniang
     * @DataTime: 2020-11-02 15:57
     */
    public function getOutFoodsStatusAttr($value,$data){

        $status = 0;

        if(isset($data['order_type'])&&in_array($data['order_type'],[0,2])&&isset($data['pay_type'])&&$data['pay_type']==1){

            $transporter_model = new TransporterOrder();

            $transporter_info  = $transporter_model->orderInfo(['order_id'=>$data['id']]);
            //等于0就是商家未接单
            if(empty($transporter_info['status'])){

                $status =  1;
            }

        }

        return $status;
    }

    /**
     * @param $value
     * @param $data
     * @功能说明:门店电话
     * @author chenniang
     * @DataTime: 2020-10-22 17:58
     */
    public function getStoreMobileAttr($value,$data){

        if(!empty($data['store_id'])){

            $store_model = new Store();

            $mobile = $store_model->where(['id'=>$data['store_id']])->value('mobile');

            return $mobile;
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-10 14:46
     * @功能说明:子订单
     */
    public function getOrderGoodsAttr($value,$data){

        if(!empty($data['id'])){

            $order_goods_model = new OrderGoods();

            $arr = $order_goods_model->orderList(['order_id'=>$data['id']]);

            return $arr;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-22 17:36
     * @功能说明:查询商品总数量
     */
    public function getTotalNumAttr($value,$data){

        if(!empty($data['id'])){

            $order_goods_model = new OrderGoods();

            $arr = $order_goods_model->where(['order_id'=>$data['id']])->sum('goods_num');

            return $arr;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function orderAdd($data){

        $data['create_time'] = time();

//        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function orderUpdate($dis,$data){

//        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-10 15:43
     * @功能说明:后台订单列表
     */
    public function adminOrderList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_restaurant_store b','a.store_id = b.id')
                ->join('longbing_card_user_info c','a.staff_id = c.fans_id')
                ->join('longbing_card_user d','a.user_id = d.id')
                ->join('longbing_card_restaurant_order_address e','a.id = e.order_id','left')
                ->join('longbing_card_restaurant_order_goods f','f.order_id = a.id','left')
                ->where($dis)
                ->field(['a.*','b.title','c.name as staff_name','d.nickName','e.name as user_name','e.phone'])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['company_info'] = getCompanyAndTop($v['company_id']);

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function orderList($dis,$mapor,$where=[]){

        $data = $this->alias('a')
            ->join('longbing_card_restaurant_store b','a.store_id = b.id','left')
            ->join('longbing_card_restaurant_order_goods f','f.order_id = a.id','left')
            ->where($dis)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field(['a.*','b.title'])
            ->group('a.id')
            ->order('a.id desc')
            ->paginate(10)
            ->toArray();
        return $data;

    }




    /**
     * @author chenniang
     * @DataTime: 2020-11-03 09:44
     * @功能说明:员工端列表
     */
    public function staffOrderList($dis,$mapor){

        $data = $this->alias('a')
            ->join('longbing_card_restaurant_store b','a.store_id = b.id','left')
            ->join('longbing_card_restaurant_order_goods f','f.order_id = a.id','left')
            ->join('longbing_card_restaurant_transporter_order g','g.order_id = a.id','left')
            ->where($dis)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->field(['a.*','b.title','g.status as send_type','g.type as distribution_type'])
            ->group('a.id')
            ->order('a.id desc')
            ->paginate(10)
            ->toArray();

        $refund_model = new RefundOrder();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $dis = [

                    'status'     => 1,

                    'order_id'   => $v['id'],

                    'user_cancel'=> 0
                ];

                $v['refund_id'] = $refund_model->where($dis)->column('id');

                if(!empty($v['refund_id'])){

                    $v['refund_id'] = array_values( $v['refund_id']);
                }

            }

        }

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:详情
     */
    public function orderInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 09:59
     * @功能说明:今日订单笔数
     */
    public function storeOrderTodayCount($store_id,$uniacid){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid
        ];


        $price = $this->where($dis)->where('pay_type','>',0)->whereDay('pay_time')->count();

        return $price;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 09:59
     * @功能说明:今日订单笔数
     */
    public function storeOrderYesterdayCount($store_id,$uniacid){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid
        ];

        $price = $this->where($dis)->where('pay_type','>',0)->whereTime('pay_time','yesterday')->count();

        return $price;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-13 09:59
     * @功能说明:今日付款笔数
     */
    public function storeOrderTodayUser($store_id,$uniacid){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid
        ];

        $price = $this->where($dis)->where('pay_type','>',0)->group('user_id')->whereDay('pay_time')->count();

        return $price;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-13 09:59
     * @功能说明:昨天
     */
    public function storeOrderYesterdayUser($store_id,$uniacid){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid
        ];

        $price = $this->where($dis)->where('pay_type','>',0)->group('user_id')->whereTime('pay_time','yesterday')->count();

        return $price;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 09:59
     * @功能说明:今日订单笔数
     */
    public function storeOrderTodayPrice($store_id,$uniacid){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid
        ];

        $price = $this->where($dis)->where('pay_type','>',0)->whereDay('pay_time')->sum('pay_prcie');

        return $price;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-13 09:59
     * @功能说明:昨天
     */
    public function storeOrderYesterdayPrice($store_id,$uniacid){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid
        ];

        $price = $this->where($dis)->where('pay_type','>',0)->whereTime('pay_time','yesterday')->sum('prcie');

        return $price;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 10:41
     * @功能说明:
     *
     */
    public function storeOderCount($store_id,$day,$uniacid,$type=0){

        $dis = [

            'uniacid'  => $uniacid,

        ];

        if(!empty($store_id)){

            $dis['store_id']  = $store_id;
        }

        if(!empty($day)){

            $dis['day']  = $day;
        }

        if($type==0){
            //订单笔数
            $price = $this->where($dis)->where('transaction_id','<>',0)->count();

        }elseif($type==1){
            //付款人数
            $price = $this->where($dis)->where('transaction_id','<>',0)->group('user_id')->count();

        }elseif($type==2){
            //订单金额
            $price = $this->where($dis)->where('transaction_id','<>',0)->sum('pay_price');

            $price = round($price,2);

        }

        return $price;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-05 14:53
     * @功能说明:查询员工单量
     */
    public function staffOrderCount($staff_id,$type=0){

//        $dis = [
//
//            'staff_id' => $staff_id,
//
//            'pay_type' => 7
//        ];

        $dis[] = ['staff_id','=',$staff_id];

        $dis[] = ['transaction_id','<>',0];

        if($type==0){

            $data = $this->where($dis)->count();

        }elseif ($type==1){

            $data = $this->where($dis)->whereDay('create_time')->count();

        }else{

            $data = $this->where($dis)->whereDay('create_time','yesterday')->count();

        }

        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-05 14:53
     * @功能说明:查询员工单量
     */
    public function staffOrderPrice($staff_id,$type=0){

//        $dis = [
//
//            'staff_id' => $staff_id,
//
//            'pay_type' => 7
//        ];

        $dis[] = ['staff_id','=',$staff_id];

        $dis[] = ['transaction_id','<>',0];

        if($type==0){

            $data = $this->where($dis)->sum('pay_price');

            $id   = $this->where($dis)->column('id');

        }elseif ($type==1){

            $data = $this->where($dis)->whereDay('create_time')->sum('pay_price');

            $id   = $this->where($dis)->whereDay('create_time')->column('id');


        }else{

            $data = $this->where($dis)->whereDay('create_time','yesterday')->sum('pay_price');

            $id   = $this->where($dis)->whereDay('create_time','yesterday')->column('id');


        }

        $refund_model = new RefundOrder();

        $refund_price = $refund_model->where(['status'=>2])->where('order_id','in',$id)->sum('refund_price');

        return round($data-$refund_price,2);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-05 14:53
     * @功能说明:查询员工服务人数
     */
    public function staffUserPrice($staff_id,$type=0){

//        $dis = [
//
//            'staff_id' => $staff_id,
//
//            'pay_type' => 7
//        ];

        $dis[] = ['staff_id','=',$staff_id];

        $dis[] = ['transaction_id','<>',0];

        if($type==0){

            $data = $this->where($dis)->sum('user_num');

        }elseif ($type==1){

            $data = $this->where($dis)->whereDay('create_time')->sum('user_num');

        }else{

            $data = $this->where($dis)->whereDay('create_time','yesterday')->sum('user_num');

        }

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-13 11:02
     * @功能说明:统计
     */
    public function storeOrderFrom($store_id,$day,$uniacid){

        $refund_order              = new RefundOrder();
        //订单笔数
        $data['order_count']       = $this->storeOderCount($store_id,$day,$uniacid);
        //下单人数
        $data['order_user']        = $this->storeOderCount($store_id,$day,$uniacid,1);
        //下单金额
        $data['order_price']       = $this->storeOderCount($store_id,$day,$uniacid,2);
        //退款订单
        $data['refund_order_count']= $refund_order->storeOderCount($store_id,$day,$uniacid);
        //退款金额
        $data['refund_order_price']= $refund_order->storeOderCount($store_id,$day,$uniacid,2);
        //实际金额
        $data['true_price']        = round( $data['order_price'] - $data['refund_order_price'],2);
        //有效订单数量
        $data['true_count']        = $this->trueOderCount($store_id,$day,$uniacid);

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 10:41
     * @功能说明:
     *
     */
    public function trueOderCount($store_id,$day,$uniacid){

        $dis = [

            'uniacid'  => $uniacid,

        ];

        if(!empty($store_id)){

            $dis['store_id']  = $store_id;
        }

        if(!empty($day)){

            $dis['day']  = $day;
        }

        //订单笔数
        $price = $this->where($dis)->where('pay_type','>',0)->count();


        return $price;
    }


    /**
     * @param $day1
     * @param $day2
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-10-13 11:25
     */
    public function diffBetweenTwoDays ($day1, $day2)
    {



        return ceil(($day1 - $day2) / 86400);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 14:56
     * @功能说明:会员订单
     */
    public function memberCount($store_id,$uniacid,$type){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid,

            'is_member'=> $type

        ];

        $count = $this->where($dis)->where('pay_type','>',0)->count();

        return $count;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 14:56
     * @功能说明:会员订单
     */
    public function typeCount($store_id,$uniacid,$type){

        $dis = [

            'store_id' => $store_id,

            'uniacid'  => $uniacid,

            'order_type'=> $type

        ];

        $count = $this->where($dis)->where('pay_type','>',0)->count();

        return $count;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 16:54
     * @功能说明:
     */
    public function fromOrder($store_id,$uniacid){
        //0是外卖 1是堂食 2是自提
        $info = [0,1,2];

        $num  = 0;

        foreach ($info as $k=>$v){
            //类型
            $data[$k]['type'] = $v;

            $data[$k]['count']=$this->typeCount($store_id,$uniacid,$v);

            $num += $data[$k]['count'];
        }
        //
        $count = 0;

        foreach ($data as &$v){

            $v['balance'] = $num > 0 ? round($v['count']/$num*100,2):0;
            //主要是为了相加可能不是100
            if($v['type'] == 2 ){

                $v['balance'] = 100 - $count;
            }

            $count += $v['balance'];
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 17:14
     * @功能说明:
     */
    public function memberOder($store_id,$uniacid){

        //0是非会员 1是会员
        $info = [0,1];

        $num  = 0;

        foreach ($info as $k=>$v){
            //类型
            $data[$k]['is_member'] = $v;

            $data[$k]['count'] = $this->memberCount($store_id,$uniacid,$v);

            $num += $data[$k]['count'];
        }
        $count = 0;

        foreach ($data as &$vs){

            $vs['balance'] = $num > 0 ? round($vs['count']/$num*100,2):0;
            //主要是为了相加可能不是100
            if($vs['is_member'] == 1){

                $vs['balance'] = 100 - $count;
            }

            $count += $vs['balance'];

        }

        return $data;
    }



    /**
     * @author chenniang
     * @DataTime: 2020-10-19 18:06
     * @功能说明:支付回调
     */
    public function orderResult($order_code,$transaction_id=''){

        $order = $this->orderInfo(['order_code'=>$order_code]);

        if(!empty($order)&&$order['pay_type']==0){

            Db::startTrans();
            //堂食订单付款就是完成
            $update['pay_type'] = 1;

            $update['pay_time'] = time();

            $update['transaction_id'] = $transaction_id;

            $this->orderUpdate(['id'=>$order['id']],$update);

            $order_goods_model = new OrderGoods();
            //修改子订单状态
            $order_goods_model->orderUpdate(['order_id'=>$order['id']],['pay_type'=>1]);
            //余额支付
            if($order['balance']>0){

                $member_model = new Member();

                $member = $member_model->getMemberInfo(['user_id'=>$this->getUserId()]);

                $store_where[] = ['user_id','=',$this->getUserId()];

                $store_where[] = ['stored','>=',$order['balance']];
                //修改余额
                $res = $member_model->where($store_where)->update(['stored'=>$member['stored']-$order['balance']]);

                if($res!=1){

                    return ['code'=>500,'msg'=> '订单支付失败'];
                }

            }
            //录入订单进度
            $pro_inser = [

                'uniacid'    => $order['uniacid'],

                'order_code' => $order['order_code'],

                'msg'        => '支付成功',

                'code'       => 1,

                'time'       => time()
            ];

            $progress_model = new Progress();

            $progress_model->dataAdd($pro_inser);
            //增加骑手表信息
            $transporter_model = new TransporterOrder();
            //初始化
            $transporter_model->initOrder($order);
            //堂食订单直接完成
            if($order['order_type']==1){

                $this->endOrder($order['id']);
            }

            $p = new Printer();
            //打印小票
            $p->printer($order['id']);


            Db::commit();

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-23 15:11
     * @功能说明:检测订单 过期则取消
     */
    public function checkOrder($uniacid,$user_id = 0){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);
        //如果未设置就默认30分钟
        $over_time = !empty($config['order_over_time'])?$config['order_over_time']:30*60;

        $dis[]  = ['uniacid','=',$uniacid];

        $dis[]  = ['pay_type','=',0];

        $dis[]  = ['check_out','=',0];

        $dis[]  = ['create_time','<',time()-$over_time];

        if(!empty($user_id)){

            $dis[] = ['user_id','=',$user_id];
        }

        $order_id = $this->where($dis)->column('id');

        if(!empty($order_id)){

            foreach ($order_id as $value){

                $this->cancelOrder($value);

            }
        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-23 15:12
     * @功能说明:取消订单
     */
    public function cancelOrder($order_id,$payConfig=[]){

        $dis = [

            'id' => $order_id
        ];

        $order_goods_model = new OrderGoods();

        $order = $this->orderInfo($dis);

        if($order['pay_type']>1){

            return ['code'=>500,'msg'=>'订单状态错误'];

        }

        Db::startTrans();
        //修改订单状态
        $res = $this->orderUpdate($dis,['cancel_time'=>time(),'pay_type'=> -1]);

        if($res!=1){

            return ['code'=>500,'msg'=>'退款人数过多请稍后重试'];
        }
        //归还库存等
        $res = $order_goods_model->refundOrder($order['order_goods']);

        if(!empty($res['code'])){

            return ['code'=>500,'msg'=>$res['msg']];
        }

        if($order['pay_price']<=0){

            return ['code'=>500,'msg'=>'订单金额为0，不用退款'];

        }
        //如果付款了就退钱
        if($order['pay_type']==1){

            $refund_model = new RefundOrder();
            //增加到退款订单中
            $refund_model->cancelOrder($order);

            $refund_order_id = $refund_model->getLastInsID();

            if($order['pay_price'] == $order['balance']){

                $member_model = new Member();

                $member = $member_model->getMemberInfo(['user_id'=>$order['user_id']]);
                //退回余额
                $res    = $member_model->memberUpdate(['user_id'=>$order['user_id']],['stored'=>$member['stored']+$order['balance']]);

                if($res!=1){

                    return ['code'=>500,'msg'=>'退款失败,请重试'];
                }
                //余额退款
                $response = [

                    'return_code' => 'SUCCESS',

                    'result_code' => 'SUCCESS'
                ];

            }else{
                //微信退款
                $response = orderRefundApi($payConfig,$order['pay_price'],$order['pay_price'],$order['transaction_id']);

            }
            //如果退款成功修改一下状态
            if ( isset( $response[ 'return_code' ] ) && isset( $response[ 'result_code' ] ) && $response[ 'return_code' ] == 'SUCCESS' && $response[ 'result_code' ] == 'SUCCESS' ) {

                $response['out_refund_no'] = !empty($response['out_refund_no'])?$response['out_refund_no']:$order['order_code'];

                $refund_model->orderUpdate(['id'=>$refund_order_id],['out_refund_no'=>$response['out_refund_no']]);

            }else {
                //失败就报错
                $discption = !empty($response['err_code_des'])?$response['err_code_des']:$response['return_msg'];

                return ['code'=>500,'msg'=> $discption];

            }

        }

        $progress_model = new Progress();
        //添加进度
        $progress_model->cancelOrder($order['uniacid'],$order['order_code']);

        Db::commit();

        return true;


    }

    /**
     * @param $uniacid
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-11-13 16:54
     */

    public function checkEndOrder($uniacid){

        $config_model = new Config();

        $config = $config_model->configInfo(['uniacid'=>$uniacid]);

        $over_success_time = $config['order_success_time'];

        $over_success_time = !empty($over_success_time)?$over_success_time*3600:3600;

        $over_success_time = 3*60;

        $dis[]  = ['b.finish_time','<',time()-$over_success_time];

        $dis[]  = ['a.pay_type','=',1];

        $dis[]  = ['b.status','=',4];

        $dis[]  = ['a.uniacid','=',$uniacid];

        $order  = $this->alias('a')
                    ->join('longbing_card_restaurant_transporter_order b','a.id = b.order_id')
                    ->where($dis)
                    ->column('a.id');

        if(!empty($order)){

            foreach ($order as $value){

                $this->endOrder($value);
            }
        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-27 13:54
     * @功能说明:完成订单时执行
     */
    public function endOrder($order_id,$user_id = ''){

        $update = [

            'pay_type' => 7,

            'hx_user'  => $user_id,

            'end_time' => time()
        ];

        Db::startTrans();
        //修改订单状态
        $this->where(['id'=>$order_id])->update($update);

        $order_goods_model = new OrderGoods();
        //修改子订单
        $order_goods_model->where(['order_id'=>$order_id])->update(['pay_type'=>7]);

        $order = $this->orderInfo(['id'=>$order_id]);

        $progress_model = new Progress();
        //添加订单完成进度
        $progress_model->endOrder($order['uniacid'],$order['order_code']);

        Db::commit();

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-27 15:42
     * @功能说明:订单自提码
     */
    public function orderQr($input,$uniacid){

        $data = longbingCreateWxCode($uniacid,$input,$input['page']);

        $data = transImagesOne($data ,['qr_path'] ,$uniacid);

        $qr   = $data['qr_path'];

        return $qr;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-29 14:18
     * @功能说明:获取订单配送费
     */
    public function getDeliverFee($order){

//        $context = new Context('',$order['uniacid'],$order['store_id']);
//        //获取配送费并返回订单
//        $order   = $context->getDeliverFee($order['order_code']);

        $delivery_model = new Delivery();

        $order = $delivery_model->getFeeOrder($order);

        return $order;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-03 16:24
     * @功能说明:查询一下当前用户是否有未支付的堂食订单
     */
    public function houseNoPayOrder($user_id,$table_id){

        $dis = [

            'user_id'   => $user_id,

            'pay_type'  => 0,

            'check_out' => 1,

            'order_type'=> 1,

            'table_id'  => $table_id,
        ];

        $order_id = $this->where($dis)->value('id');

        return $order_id;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-03 16:47
     * @功能说明:判断当前桌号是否正在就餐
     */
    public function tableHaveOrder($table_id,$user_id){

        $dis = [

            'table_id'  => $table_id,

            'pay_type'  => 0,

            'check_out' => 1,

            'order_type'=> 1
        ];

        $order_id = $this->where($dis)->where('user_id','<>',$user_id)->value('id');

        return $order_id;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-03 17:27
     * @功能说明:处理加菜订单
     */
    public function addFoodsOrder($order_id,$order_info){

        //老的订单
        $dis = [

            'id'        => $order_id,

            'pay_type'  => 0,

            'check_out' => 1,

            'order_type'=> 1,
        ];

        $old_order = $this->orderInfo($dis);

        if(empty($old_order)){

            return ['code'=>500,'msg'=>'订单不存在或状态错误，无法加菜'];
        }

        $update = [

            'init_price'          => $old_order['init_price']+$order_info['init_price'],

            'goods_price'         => $old_order['goods_price']+$order_info['goods_price'],

            'init_goods_price'    => $old_order['init_goods_price']+$order_info['init_goods_price'],

            'pay_price'           => $old_order['pay_price']+$order_info['pay_price'],

            'member_price'        => $old_order['member_price']+$order_info['member_price'],

            'total_feeding_price' => $old_order['total_feeding_price']+$order_info['total_feeding_price'],

            'order_text'          => $order_info['order_text'],
            //加菜次数
            'add_foods_time'      => $old_order['add_foods_time']+1,

            'confirm'             => 0,
        ];

        $res = $this->orderUpdate($dis,$update);

        if($res!=1){

            return ['code'=>500,'msg'=>'加菜失败'];
        }

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-03 17:51
     * @功能说明:检查下单
     */
    public function checkPayOrder(){




    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-09 15:56
     * @功能说明:待确认订单数量
     */
    public function confirmCount($staff_id){

        $staff_model = new StoreStaff();

        $store = $staff_model->getStaffStore($staff_id,3);
        //员工端
        if($store['role']==0){

            $dis[] = ['staff_id','=',$staff_id];

        }else{
            //管理员端
            $dis[] = ['store_id','=',$store['store_id']];
        }

        $dis[] = ['confirm','=',0];

        $dis[] = ['order_type','=',1];

        $mapor[] = ['check_out','=',1];

        $mapor[] = ['pay_type','>',0];

        $count = $this->where($dis) ->where(function ($query) use ($mapor){
            $query->whereOr($mapor);
        })->count();

        return $count;

    }




}